<?php

namespace App\Http\Controllers\api;

use App\Models\Book;
use Illuminate\Support\Facades\Input;
use App\Models\Category;
use App\Models\Operation;

class BookController extends BaseController {
	
	public function index() {
		$books = Book::paginate(50);
		return view('backend.books', compact('books'));
	}
	
	public function store() {
		$input = Input::all();
		$category = Category::where('name', $input['category'])->first();
		if(is_null($category)) {
			$category = Category::create([
				'name' => $input['category']
			]);
		}
		unset($input['category']);
		$input['category_id'] = $category->id;
		$book = Book::where('name', $input['name'])->first();
		if(is_null($book)) {
			$book = Book::create($input);
			Operation::create([
				'action' => '添加书籍',
				'content' => $book->name
			]);
		}
		return 200;
	}
}
